﻿using Globalization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Tasks;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Search;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.ApplicationSettings;
using Windows.UI.Notifications;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace JalaliCalendar
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        int myYear;
        YearControl myYearControl = null;

        Rect _windowBounds;
        double _settingsWidth = 346;
        Popup _settingsPopup;

        public MainPage()
        {
            this.InitializeComponent();

            // Initialized Settings
            if (!ApplicationData.Current.LocalSettings.Values.Keys.Contains("HijriAdjustment"))
                ApplicationData.Current.LocalSettings.Values.Add("HijriAdjustment", -1);
            if (!ApplicationData.Current.LocalSettings.Values.Keys.Contains("ShowHolidays"))
                ApplicationData.Current.LocalSettings.Values.Add("ShowHolidays", true);

            _windowBounds = Window.Current.Bounds;
            SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;

            myYear = new PersianCalendar().GetYear(DateTime.Today);
            ShowYear();
        }

        private void ShowYear()
        {
            CalendarConversion cc = new CalendarConversion();
            cc.setIranianDate(myYear, 1, 1);
            if (myYearControl != null)
                MainGrid.Children.Remove(myYearControl);

            myYearControl = new YearControl(myYear, (cc.getDayOfWeek() + 2) % 7);
            Grid.SetRow(myYearControl, 0);
            Grid.SetRowSpan(myYearControl, 2);
            Canvas.SetZIndex(myYearControl, -1);
            MainGrid.Children.Add(myYearControl);

            //Storyboard storyBoard = new Storyboard();
            //DoubleAnimation opacityAnimation = new DoubleAnimation
            //{
            //    From = 0.0,
            //    To = 1.0,
            //    Duration = TimeSpan.FromSeconds(0.2)
            //};
            //Storyboard.SetTarget(opacityAnimation, myYearControl);
            //Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
            //storyBoard.Children.Clear();
            //storyBoard.Children.Add(opacityAnimation);
            //storyBoard.Begin();
        }

        void onSettingsCommand(IUICommand command)
        {
            _settingsPopup = new Popup();
            _settingsPopup.Closed += OnPopupClosed;
            Window.Current.Activated += OnWindowActivated;
            _settingsPopup.IsLightDismissEnabled = true;
            _settingsPopup.Width = _settingsWidth;
            _settingsPopup.Height = _windowBounds.Height;

            UserControl mypane = new UserControl();
            if (command.Id == "aboutSettings")
                mypane = new About();
            else
                mypane = new Options();

            mypane.Width = _settingsWidth;
            mypane.Height = _windowBounds.Height;

            _settingsPopup.Child = mypane;
            _settingsPopup.SetValue(Canvas.LeftProperty, _windowBounds.Width - _settingsWidth);
            _settingsPopup.SetValue(Canvas.TopProperty, 0);
            _settingsPopup.IsOpen = true;
        }

        private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {
                _settingsPopup.IsOpen = false;
            }
        }

        void OnPopupClosed(object sender, object e)
        {
            Window.Current.Activated -= OnWindowActivated;
            ShowYear();
        }
        
        void onCommandsRequested(
            SettingsPane settingsPane,
            SettingsPaneCommandsRequestedEventArgs eventArgs)
        {
            UICommandInvokedHandler handler = new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand aboutCommand = new SettingsCommand(
                "aboutSettings", "About", handler);

            foreach (SettingsCommand sc in eventArgs.Request.ApplicationCommands)
                if (sc.Label == aboutCommand.Label)
                    return;

            SettingsCommand optionsCommand = new SettingsCommand(
                "optionsSettings", "Options", handler);

            eventArgs.Request.ApplicationCommands.Add(aboutCommand);
            eventArgs.Request.ApplicationCommands.Add(optionsCommand);
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Button_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            myYear++;
            ShowYear();
        }

        private void Button_Tapped_2(object sender, TappedRoutedEventArgs e)
        {
            myYear--;
            ShowYear();
        }
    }
}
